package com.clear.strategy;

import org.junit.Test;

import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
//        // 定义一个调用方
//        Calculator calculator = new Calculator();
//
//        // 计算加法
//        Addition addition = new Addition();
//        calculator.setOperation(addition);
//        System.out.println("10 + 5 = " + calculator.executeOperation(10, 5));
//
//        // 计算减法
//        calculator.setOperation(new Subtraction());
//        System.out.println("10 - 5 = " + calculator.executeOperation(10, 5));
//
//        // 计算乘法
//        calculator.setOperation(new Multiplication());
//        System.out.println("10 * 5 = " + calculator.executeOperation(10,5));


    }


    @Test
    public void test2(){
        Calculator calculator = new Calculator();
        Scanner scanner = new Scanner(System.in);
        double num1 = 1.2, num2 = 1.3;

        while (true) {
            String next = scanner.next();
            switch (next) {
                case "+":
                    calculator.setOperation(new Addition());
                    break;
                case "-":
                    calculator.setOperation(new Subtraction());
                    break;
                case "*":
                    calculator.setOperation(new Multiplication());
                    break;
                default:
                    throw new IllegalArgumentException("输入的操作符非法");
            }
            System.out.println(calculator.executeOperation(num1, num2));
        }
    }

    /**
     * 工厂+策略 优化if-else分支语句
     */
    @Test
    public void test3(){
        Calculator calculator = new Calculator();
        Scanner scanner = new Scanner(System.in);
        double num1 = 1.2, num2 = 1.3;

        while (true) {
            String type = scanner.next();
            // 需要对type做合法校验，此处省略
            // 从策略工厂拿到具体的策略
            Operation operation = OperationFactory.getOperation(type);
            calculator.setOperation(operation);

            System.out.println(calculator.executeOperation(num1, num2));
        }
    }
}
